package aktor;

import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;

public class DataBaseConnection {

//    static String jdbcURL = "jdbc:oracle:thin:@localhost:1521:XE";
//    static String user = "hr";
//    static String password = "hr";
//    static Connection connection;
    
     static String jdbcURL = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
    static String user = "MHS105314091";
    static String password = "MHS105314091";
    static Connection connection;

    public DataBaseConnection() {
//        try {
//            Class.forName("oracle.jdbc.driver.OracleDriver");
//            connection = DriverManager.getConnection(jdbcURL, user, password);
//            System.out.println("koneksi berhasil");
//        } catch (Exception ex) {
//            System.out.println("message: " + ex.getMessage());
//        }
    }

    public static Connection getDBConnection() throws SQLException {

        try {
            OracleDataSource ds;
            ds = new OracleDataSource();

            ds.setURL(jdbcURL);
            connection = (Connection) ds.getConnection(user, password);
            System.out.println("Koneksi berhasil");
            return connection;
        } catch (SQLException ex) {
            System.out.println("Koneksi gagal");
            return null;
        }
    }

    public boolean isConnected() {
        if (connection != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean getClosed() {
        if (isConnected()) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException ex) {
            }
            return true;
        } else {
            return false;
        }
    }

    public String getJdbcURL() {
        return jdbcURL;
    }

    public void setJdbcURL(String jdbcURL) {
        DataBaseConnection.jdbcURL = jdbcURL;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        DataBaseConnection.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        DataBaseConnection.password = password;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConn(Connection connection) {
        DataBaseConnection.connection = connection;
    }

//    public static void main(String args[]) {
//        DataBaseConnection conn = new DataBaseConnection();
//        try {
//            String query = "select * from pasien";
//            Statement statement = conn.getConnection().createStatement();
//            ResultSet result = statement.executeQuery(query);
//
//            while (result.next()) {
//                System.out.println("userName : " + result.getString("nama"));
//                System.out.println("emailAddress  : " + result.getString("email"));
//            }
//            statement.close();
//        } catch (Exception ex) {
//            System.out.println("message: " + ex.getMessage());
//        }
//    }
    public static void main(String[] args) throws SQLException {
        DataBaseConnection conn = new DataBaseConnection();
        DataBaseConnection.getDBConnection();
        conn.getClosed();
//        System.out.println(Integer.MAX_VALUE);
    }
}
